package cn.tedu.charging.account.service.impl;

import cn.tedu.charging.account.dao.cache.IUserCacheRepository;
import cn.tedu.charging.account.dao.persist.repository.IUserRepository;
import cn.tedu.charging.account.pojo.entity.User;
import cn.tedu.charging.account.pojo.param.UserAddNewParam;
import cn.tedu.charging.account.pojo.vo.UserLoginResultVO;
import cn.tedu.charging.account.pojo.vo.UserStandardVO;
import cn.tedu.charging.account.service.IUserService;
import cn.tedu.charging.common.ex.ServiceException;
import cn.tedu.charging.common.po.UserLoginInfoPO;
import cn.tedu.charging.utils.JwtUtil;
import cn.tedu.charging.utils.WxCryptUtils;
import cn.tedu.charging.web.JsonResult;
import cn.tedu.charging.web.ServiceCode;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Jwts;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理用户数据的业务实现类
 *
 * @author wyy
 * @version 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    public UserServiceImpl() {
        log.debug("创建业务类对象：UserServiceImpl");
    }

    @Autowired
    private IUserRepository userRepository;

    @Autowired
    private IUserCacheRepository userCacheRepository;

    @Override
    public UserLoginResultVO login(String openId) {
        log.debug("开始处理【用户登录】的业务，参数：{}", openId);
        UserStandardVO userStandardVO = userRepository.selectByOpenId(openId);
        String id = userStandardVO.getId();
        // 生成JWT之后，存入到Redis中
        String jwt = JwtUtil.generate(id);
        UserLoginInfoPO userLoginInfoPO = new UserLoginInfoPO();
        BeanUtils.copyProperties(userStandardVO, userLoginInfoPO);
        userCacheRepository.saveLogin(jwt, userLoginInfoPO);
        UserLoginResultVO userLoginResultVO = new UserLoginResultVO();
        userLoginResultVO.setId(userStandardVO.getId());
        userLoginResultVO.setNickname(userStandardVO.getNickname());
        userLoginResultVO.setAvatar(userStandardVO.getAvatar());
        userLoginResultVO.setToken(jwt);
        // Map<String, Object> claims = new HashMap<>();
        // claims.put("user", userLoginResultVO);
        // claims.put("token", jwt);
        return userLoginResultVO;
    }

    @Override
    public UserLoginResultVO sign(Map<String, Object> userInfo, String openId) {
        UserAddNewParam userAddNewParam = new UserAddNewParam();
        userAddNewParam.setNickname((String) userInfo.get("nickname"));
        userAddNewParam.setAvatar((String) userInfo.get("avatarUrl"));
        userAddNewParam.setSex((Integer) userInfo.get("gender"));
        userAddNewParam.setOpenId(openId);
        userRepository.insert(userAddNewParam);

        String token = JwtUtil.generate(userAddNewParam.getId());

        UserLoginInfoPO userLoginInfoPO = new UserLoginInfoPO();
        BeanUtils.copyProperties(userAddNewParam, userLoginInfoPO);

        UserLoginResultVO resultVO = new UserLoginResultVO();
        BeanUtils.copyProperties(userAddNewParam, resultVO);
        resultVO.setToken(token);

        userCacheRepository.saveLogin(token, userLoginInfoPO);

        return resultVO;
    }

    @Override
    public void delete(String id) {
        log.debug("开始处理【根据ID删除用户】的业务，参数：{}", id);
        int rows = userRepository.deleteById(id);
        log.debug("已经删除{}条数据", rows);


    }

    @Override
    public UserStandardVO getStandardByPhone(String phone) {
        log.debug("开始处理【根据ID查询用户】业务，参数：{}", phone);
        UserStandardVO userStandardVO = userRepository.countByPhone(phone);
        return userStandardVO;
    }

    @Override
    public UserStandardVO getStandardByOpenId(String openId) {
        log.debug("开始处理【根据openId查询用户】业务，参数：{}", openId);
        UserStandardVO userStandardVO = userRepository.selectByOpenId(openId);
        return userStandardVO;
    }

    @Override
    public void update(User user) {
        int i = userRepository.updateById(user);
        log.debug("修改了{}条数据", i);
    }
}
